package com.example.udpLearn.udp3;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * 客户端不断发送数据，服务端不断接收数据：客户端
 */
public class Client {
    public static void main(String[] args) throws IOException {

        System.out.println("客户端启动...");

        /**
         * 定义客户端对象
         */
        DatagramSocket socket = new DatagramSocket();

        /**
         * 发送数据
         */
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要发送的数据：");
            String string = scanner.nextLine();
            if ("exit".equals(string)) {
                System.out.println("客户端退出...");
                socket.close();
                break;
            }
            byte[] bytes = string.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8888);
            socket.send(packet);
        }

    }
}
